package sis.studentinfo;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-28 16:10
 * @notify
 * @version 1.0
 */

import java.io.*;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;

public class StudentUI {
    public static final String MENU = "(A)添加学生 or (Q)退出?";
    public static final String ADD_OPTION = "A";
    public static final String QUIT_OPTION = "Q";
    public static final String NAME_PROMPT = "姓名: ";
    public static final String ADDED_MESSAGE = "添加成功";

    private BufferedReader reader;

    private BufferedWriter writer;
    private List<Student> students = new ArrayList<>();

    public StudentUI(){
        this.reader = new BufferedReader(new InputStreamReader(System.in));
        this.writer = new BufferedWriter(new OutputStreamWriter(System.out));
    }

    public StudentUI(BufferedReader reader, BufferedWriter writer) {
        this.reader = reader;
        this.writer = writer;
    }

    public void run() throws IOException {
        String line;
        do {
            write(MENU);
            line = reader.readLine();
            if (line.equals(ADD_OPTION)) {
                addStudent();
            }
        } while (!line.equals(QUIT_OPTION));
    }
    private void write(String line) throws IOException {
        writer.write(line, 0, line.length());
        writer.flush();
    }
    public List<Student> getAddedStudents() {
        return students;
    }

    private void addStudent() throws IOException {
        write(NAME_PROMPT);
        String name = reader.readLine();
        students.add(new Student(name));
        writeln(ADDED_MESSAGE);
    }



    private void writeln(String line) throws IOException {
        write(line);
        writer.newLine();
        writer.flush();

    }
}
